import { ValidLinkProps } from '@/pages/meetingList/interface'
import requestMeeting, { request } from '@/plugins/axios'
import { fetchRegularTimeEntity, fetchRequestMeetingListParams, fetchResponseMeetingListEntity } from './interface'
import dayjs from '@/plugins/dayjs'

export const fetchGetMeetingList = async (params: fetchRequestMeetingListParams) =>
	request<fetchResponseMeetingListEntity>({
		url: `/meetings/web`,
		method: 'GET',
		params: {
			...params,
			start_time: dayjs(params.start_time).tz().format(),
			end_time: dayjs(params.end_time).tz().format(),
		},
	}).then((res) => res)
/**结束会议 */
export const fetchEndMeeting = async (meetingMoid: string, end_room: 0 | 1) =>
	requestMeeting<number>({
		url: `/confs/${meetingMoid}?end_room=${end_room}`,
		method: 'delete',
	}).then((res) => res)

export const fetchDeleteMeeting = async (meetingMoid: string) =>
	requestMeeting<number>({
		url: '/booked_confs/' + meetingMoid,
		method: 'delete',
	}).then((res) => res)

export const fetchDeleteMeetingByReason = async (meetingMoid: string, params: { reason: string }) =>
	requestMeeting({
		url: '/booked_confs/' + meetingMoid,
		method: 'delete',
		data: params,
	}).then((res) => res)

export const fetchDeleteRegularMeetingByReason = async (defaultid: string, params: {}) =>
	requestMeeting({
		url: `/regular_meeting/${defaultid}`,
		method: 'delete',
		data: params,
	}).then((res) => res)

export const fetchGetIsValidLink = async (params: ValidLinkProps) =>
	requestMeeting({
		url: `/meetings/valid_link`,
		method: 'GET',
		params: { ...params, last_modify_time: dayjs(params.last_modify_time).tz().format() },
	}).then((res) => res)

export const fetchFeedBack = async (_params: any, meeting_id: string) =>
	requestMeeting({
		url: `/meetings/${meeting_id}/feedback`,
		method: 'POST',
		data: _params,
	}).then((res) => res)

export const fetchStartMeetingNow = async (_params: { meetingMoid: string }) =>
	requestMeeting({
		url: `/meeting/startMeetingNow`,
		method: 'POST',
		data: _params,
	}).then((res) => res)

/**加载冲突会议 */
export const fetchConflictMeeting = async (meetingMoid: string) =>
	request<fetchResponseMeetingListEntity>({
		url: `/meetings/${meetingMoid}/conflict_meetings`,
		method: 'GET',
	}).then((res) =>
		res.success
			? {
					...res,
					meetings: res.meetings.map((item) => ({
						...item,
						start_time: dayjs(item.start_time).format(),
					})),
			  }
			: res
	)

/**请求所有例会 */
export const fetchRegularMeeting = async (regularMeetingMoid: string) =>
	request<fetchRegularTimeEntity>({
		url: `/regular_meeting/${regularMeetingMoid}/date`,
		method: 'GET',
	}).then((res) => ({
		...res,
		data: res.success ? res.data.map((item) => ({ ...item, day: dayjs(item.day).format('YYYY-MM-DD') })) : [],
	}))
